<template>
  <el-card>
    <el-row type="flex" justify="space-between">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="目录名称">
          <el-input v-model="form.directoryName" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.state" placeholder="请选择">
            <el-option label="启用" value="1" />
            <el-option label="禁用" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button  @click="onClear">清除</el-button>
          <el-button type="primary" @click="onSubmit">搜索</el-button>
        </el-form-item>
      </el-form>
      <el-button type="success" @click="onAdd" :icon="Edit">新增目录</el-button>
    </el-row>

    <el-row class="data">
      <el-icon class="Info"><InfoFilled /></el-icon>
      <div>数据一共{{page.total}}条</div>
    </el-row>

    <el-table :data="list" border style="width: 100%; margin-top: 15px;">
      <el-table-column type="index" width="70" label="序号">
      </el-table-column>
      <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
      <el-table-column prop="directoryName" label="目录名称"> </el-table-column>
      <el-table-column prop="username" label="创建者"> </el-table-column>
      <el-table-column label="创建日期">
        <template v-slot="{row}">
          {{ row.addDate = dateFliter(row.addDate) }}
        </template>
      </el-table-column>
      <el-table-column prop="totals" label="面试题数量"> </el-table-column>
      <el-table-column label="状态">
        <template v-slot="{row}">
          {{row.state ===1? '启用':'禁用'}}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{row}">
          <el-button type="text" @click="onEdit(row)">
            <span v-show="row.state === 1">禁用</span>
            <span v-show="row.state === 0">启用</span>
          </el-button>
          <el-button type="text" @click="onFix(row)" :disabled="row.state === 0 ? false : true">修改</el-button>
          <el-button type="text" :disabled="row.state === 0 ? false : true" @click="onDel(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-row type="flex" justify="end">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="form.page"
        :page-size="form.pagesize"
        @current-change="onCurrentPage"
        :total="page.total"
        @size-change="handleSizeChange"
        :small="small"
        :disabled="disabled"
        :page-sizes="[5, 10, 15, 20]"
      >
      </el-pagination>
    </el-row>

    <!-- 新增的弹出框 -->
    <el-dialog v-model="showDialog" :title="title" width="400px">
      <AddDirectorys ref="directorysRef" v-if="showDialog"  @close="showDialog = false" :fixForm="fixForm" @resetList="get"></AddDirectorys>
    </el-dialog>
  </el-card>
</template>

<script>
import dayjs from 'dayjs'
import { onMounted, reactive, ref, nextTick } from 'vue'
import { list as getList, changeState, remove, detail } from '@/api/hmmm/directorys.js'
import AddDirectorys from './addDirectorys.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  Edit,
  InfoFilled
} from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
export default {
  setup () {
    const onSubmit = () => {
      get()
      form.page = 1
    }
    const onClear = () => {
      form.directoryName = undefined
      form.state = undefined
      get()
    }
    const form = reactive({
      page: 1,
      pagesize: 5,
      directoryName: undefined,
      subjectID: undefined,
      state: undefined,
      id: undefined
    })
    const page = reactive({
      total: 0
    })
    const fixForm = reactive({
      directoryName: undefined,
      subjectID: undefined,
      id: undefined
    })
    const list = ref([])
    const dateFliter = (val, format = 'YYYY-MM-DD hh:mm:ss') => {
      if (!isNaN(val)) {
        val = parseInt(val)
      }
      return dayjs(val).format(format)
    }
    async function get () {
      const { data } = await getList(form)
      console.log(data)
      page.total = data.counts
      list.value = data.items
    }

    onMounted(() => {
      // console.log(111)
      // console.log(name)
      form.subjectID = name
      get()
    })
    function onCurrentPage (val) {
      form.page = val
      get()
    }

    const handleSizeChange = (val) => {
      form.pagesize = val
      get()
    }

    const showDialog = ref(false)

    const onAdd = () => {
      title.value = '新增目录'
      showDialog.value = true
    }

    const directorysRef = ref()

    const title = ref('')

    // 禁用/启用
    const onEdit = async (row) => {
      if (row.state === 1) {
        row.state = 0
      } else if (row.state === 0) {
        row.state = 1
      }
      await changeState({
        id: row.id,
        state: row.state
      })
      ElMessage.success('操作成功')
    }

    const onDel = (id) => {
      ElMessageBox.confirm(
        '此操作将永久删除该目录，请确认',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(async () => {
          await remove(id)
          get()
          ElMessage({
            type: 'success',
            message: '操作成功'
          })
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '操作取消'
          })
        })
    }

    // 修改
    const onFix = async (id) => {
      title.value = '修改目录'
      showDialog.value = true
      const res = await detail(id)
      await nextTick()
      directorysRef.value.form.directoryName = res.data.directoryName
      fixForm.id = res.data.id
    }

    const router = useRouter()

    const name = router.currentRoute.value.query.subjectName

    return {
      onSubmit,
      list,
      form,
      page,
      onCurrentPage,
      get,
      dateFliter,
      onClear,
      showDialog,
      onAdd,
      directorysRef,
      onEdit,
      onDel,
      onFix,
      fixForm,
      title,
      Edit,
      router,
      name,
      InfoFilled,
      handleSizeChange
    }
  },
  components: { AddDirectorys, InfoFilled }
}
</script>

<style lang="less" scoped>
.data {
  height: 30px;
  line-height: 32px;
  background-color: #f4f4f5;
  font-size: 13px;
  div {
    color: #9093a2;
    margin-left: 10px;
  }
}
.Info {
  margin-top: 8px;
  margin-left: 8px;
}
</style>
